<template>
  <el-container class="app-container">
    <!-- 顶部区域 -->
    <el-header class="app-header">
      <!-- 左上角快速访问区 -->
      <QuickAccess />

      <!-- 动态导航区域 -->
      <el-menu mode="horizontal" :default-active="$route.path" class="nav-menu">
        <el-menu-item
          v-for="route in menuRoutes"
          :key="fullPath(route.parentPath, route.path)"
          :index="fullPath(route.parentPath, route.path)"
          @click="$router.push(fullPath(route.parentPath, route.path))"
        >
          {{ route.meta?.title || route.name }}
        </el-menu-item>
      </el-menu>

      <!-- 工具栏区域 -->
      <TopToolbar />
    </el-header>

    <!-- 内部容器：左侧结构树 + 右侧主内容 -->
    <el-container>
      <!-- 左侧结构树 -->
      <el-aside v-if="isSidebarVisible" width="260px" class="left-structure-tree">        
        <router-view name="sidebar" v-slot="{ Component }">
          <keep-alive>
            <component :is="Component" />
          </keep-alive>
        </router-view>
      </el-aside>

      <!-- 主内容 -->
      <el-main class="main-content" :class="{ 'full-width': !isSidebarVisible }">
        <router-view v-slot="{ Component }">
          <keep-alive>
            <component :is="Component" :key="$route.path" />
          </keep-alive>
        </router-view>
      </el-main>
      <!-- 右侧面板（浮动层） -->
      <RightPanel
        :current-component="activePanelComponent"
        :is-active="isPanelActive"
        :panel-width="300"
        @close="isPanelActive = false"
      />

      <!-- 右侧垂直按键区 -->
      <RightVerticalButtons
        @toggle-panel="handleTogglePanel('Properties')"
        @toggle-tools="handleTogglePanel('Tools')"
      />
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

import QuickAccess from '../components/QuickAccess.vue'
import TopToolbar from '../components/TopToolbar.vue'
import RightVerticalButtons from '../components/VerticalButtons.vue' // 引入新组件

import RightPanel from '../components/RightPanel.vue' // 引入新组件

import PropertiesPanel from '../components/PropertiesPanel.vue' // 属性面板
import ToolsPanel from '../components/ToolsPanel.vue' // 辅助工具面板

const route = useRoute()
const router = useRouter()

// 获取 /editor/file 下的所有子路由作为菜单项
// 获取 /editor 下的所有子路由并附加 parentPath
const menuRoutes = computed(() => {
  const editorRoute = router.options.routes.find((r) => r.path === '/editor')
  console.log(editorRoute);
  const children = editorRoute?.children || []

  return children.flatMap((parent) => {
    return (parent.children || []).map((child) => ({
      ...child,
      parentPath: parent.path,
    }))
  })
})

// 动态拼接全路径
const fullPath = (parentPath: string, path: string) => {
  return `/editor/${parentPath}/${path}`
}

// 根据路由 meta 判断是否显示侧边栏容器
const isSidebarVisible = computed(() => {
  return route.meta.sidebarVisible !== false
})

// 控制右侧面板是否显示
const isPanelActive = ref(false)
const activePanelComponent = ref(PropertiesPanel)
// 显示面板（根据点击的按钮切换组件）
const handleTogglePanel = (type: string) => {
  activePanelComponent.value = type === 'Properties' ? PropertiesPanel : ToolsPanel
  isPanelActive.value = true
}
</script>

<style scoped>
.right-panel {
  position: fixed; /* 固定定位，脱离文档流 */
  top: 64px; /* 与顶部导航栏对齐 */
  right: 50px; /* 位于垂直按钮左侧（按钮宽 50px） */
  height: calc(100% - 64px); /* 减去顶部导航高度 */
  z-index: 999;
  transition: transform 0.3s ease-in-out;
  transform: translateX(100%); /* 默认隐藏在右侧 */
}

.right-panel.active {
  transform: translateX(0);
}

.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  z-index: 998;
  display: flex;
  align-items: center;
  justify-content: center;
}

.panel-content {
  position: relative;
  height: 100%;
  background: white;
  border-left: 1px solid #ebeef5;
  box-shadow: -2px 0 5px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
}

.close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 1000;
}

.vertical-buttons {
  position: fixed; /* 固定定位 */
  top: 64px;
  right: 0;
  width: 50px;
  height: calc(100% - 64px);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 16px;
  gap: 16px;
  background-color: white;
  border-left: 1px solid #ebeef5;
  box-shadow: -1px 0 3px rgba(0, 0, 0, 0.05);
  z-index: 1000; /* 确保按钮区在面板之上 */
}

/* 原有样式保持不变 */
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: row;
  font-family: 'Microsoft YaHei', sans-serif;
  background-color: #f5f7fa;
}

.left-structure-tree {
  background-color: white;
  border-right: 1px solid #ebeef5;
  box-shadow: 1px 0 3px rgba(0, 0, 0, 0.05);
}

.main-content {
  padding: 20px;
  background-color: white;
  margin: 16px 80px 16px 16px; /* 原来的右边距 + 面板区预留空间 */
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* 当侧边栏隐藏时，主内容区占满整个空间 */
.main-content.full-width {
  margin-left: 16px;
  margin-right: 16px;
  width: calc(100% - 32px); /* 撑满左右边距 */
}

.app-header {
  display: flex;
  align-items: center;
  height: 64px;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0 24px;
  position: relative;
  z-index: 100;
}

.quick-access {
  display: flex;
  align-items: center;
  margin-right: 40px;
}

.home-icon {
  color: #409eff;
  margin-right: 8px;
}

.title-text {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-right: 16px;
  white-space: nowrap;
}

.quick-actions {
  display: flex;
  gap: 6px;
}

.action-btn {
  width: 30px;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  border-color: #dcdfe6;
  transition: all 0.2s;
}

.action-btn:hover {
  background: #ecf5ff;
  border-color: #409eff;
  transform: translateY(-1px);
}

.action-btn .el-icon {
  font-size: 14px;
}

.nav-menu {
  flex: 1;
  border-bottom: none;
}

:deep(.el-menu--horizontal) {
  border-bottom: none;
  height: 64px;
  line-height: 64px;
}

:deep(.el-menu-item) {
  font-size: 14px;
  font-weight: 500;
  height: 64px;
  line-height: 64px;
  padding: 0 16px;
  border-bottom: 2px solid transparent;
  transition: border-color 0.3s;
}

:deep(.el-menu-item:hover) {
  color: #409eff;
  background: #ecf5ff;
  border-bottom-color: #409eff;
}

:deep(.el-menu-item.is-active) {
  color: #409eff;
  background: #ecf5ff;
  border-bottom-color: #409eff;
}

.top-toolbar {
  display: flex;
  margin-left: auto;
  gap: 8px;
}

.main-container {
  flex: 1;
  display: flex;
  background-color: #f0f2f5;
}

.editor-area {
  padding: 20px;
  overflow: auto;
  background-color: white;
  margin: 16px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.vertical-buttons {
  background-color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 0;
  gap: 16px;
  box-shadow: -1px 0 3px rgba(0, 0, 0, 0.05);
}

.vertical-btn {
  width: 40px;
  height: 40px;
  margin: 8px 0;
}

.assistant-panel {
  background-color: white;
  box-shadow: -1px 0 5px rgba(0, 0, 0, 0.05);
  border-left: 1px solid #ebeef5;
  overflow: auto;
}
</style>
